package com.ziyun.erp.web.modules.quoteFactoryMaterial.controller;

import com.ziyun.erp.common.utils.UserUtils;
import com.ziyun.erp.modules.quoteFactoryMaterial.entity.QuoteFactoryMaterialEntity;
import com.ziyun.erp.modules.quoteFactoryMaterial.entity.QuoteFactoryMaterialVO;
import com.ziyun.erp.modules.quoteFactoryMaterial.service.QuoteFactoryMaterialService;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;
import io.renren.modules.sys.entity.SysUserEntity;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;


/**
 * 工厂材料价格表
 *
 * @author jl
 * @email
 * @date 2018-09-12 09:04:01
 */
@RestController
@RequestMapping("quoteFactoryMaterial/quotefactorymaterial")
public class QuoteFactoryMaterialController {
    @Autowired
    private QuoteFactoryMaterialService quoteFactoryMaterialService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("quoteFactoryMaterial:quotefactorymaterial:list")
    public R list(@RequestParam Map<String, Object> params) {
        //erp的工厂id全部是1
        params.put("factoryId", 1);
        PageUtils page = quoteFactoryMaterialService.queryPage(params);
        return R.ok().put("page", page);
    }

    //页面leftTable数据
    @RequestMapping("/getLeftTableData")
    public  List<QuoteFactoryMaterialVO> getLeftTableData() {
        //erp的工厂id全部是1
        Integer factoryId=1;
        List<QuoteFactoryMaterialVO> leftTableData = quoteFactoryMaterialService.getLeftTableData(factoryId);
        return quoteFactoryMaterialService.getLeftTableData(factoryId);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("quoteFactoryMaterial:quotefactorymaterial:info")
    public R info(@PathVariable("id") Integer id) {
        QuoteFactoryMaterialEntity quoteFactoryMaterial = quoteFactoryMaterialService.selectOneById(id);

        return R.ok().put("quoteFactoryMaterial", quoteFactoryMaterial);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("quoteFactoryMaterial:quotefactorymaterial:save")
    public R save(@RequestBody List<QuoteFactoryMaterialEntity> quoteFactoryMaterialList) {
        SysUserEntity curentUser = UserUtils.getCurentUser();
        Integer factoryId = 1;

        return quoteFactoryMaterialService.insertQuoteFactoryMaterial(quoteFactoryMaterialList, factoryId,curentUser.getUsername());
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("quoteFactoryMaterial:quotefactorymaterial:update")
    public R update(@RequestBody List<QuoteFactoryMaterialEntity> quoteFactoryMaterialList) {
        SysUserEntity curentUser = UserUtils.getCurentUser();
        String username =curentUser.getUsername();
        //erp的工厂id全部是1
        Integer factoryId = 1;
        quoteFactoryMaterialService.updateQuoteFactoryMaterialById(quoteFactoryMaterialList,factoryId,username);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("quoteFactoryMaterial:quotefactorymaterial:delete")
    public R delete(@RequestBody Integer[] ids) {
        quoteFactoryMaterialService.deleteByIds(ids);

        return R.ok();
    }

    /**
     * 增加克重弹窗 根据工厂id和选项id联合查询某工厂的材料克重
     */
    @GetMapping("addWeight/{optionName}")
    public List<QuoteFactoryMaterialEntity> addWeight(@PathVariable String optionName) {
        return quoteFactoryMaterialService.findWeightList(1, optionName, "shangwuyinshua");
    }

    /**
     * 启用/禁用
     */
    @PostMapping("/setStart/{id}/{value}")
    public R setStart(@PathVariable Integer id, @PathVariable Boolean value) {
        quoteFactoryMaterialService.setStart(id, value);
        return R.ok();
    }

    //删除
    @RequestMapping("/delWeight")
    public R delWeight(String optionName) {
        //erp的工厂id全部是1
        Integer factoryId = 1;
        return quoteFactoryMaterialService.delWeight(optionName,factoryId);
    }

    //设为特种纸
    @RequestMapping("/specialPaper")
    public R specialPaper(@RequestBody Integer[] ids) {
        quoteFactoryMaterialService.specialPaper(ids);

        return R.ok();
    }

    //取消特种纸
    @RequestMapping("/unSpecialPaper")
    public R unSpecialPaper(@RequestBody Integer[] ids) {
        quoteFactoryMaterialService.unSpecialPaper(ids);

        return R.ok();
    }
}
